package com.example.chartroom.controller;

import com.example.chartroom.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class MessageSessionController {
    @Autowired
    private MessageSessionMapper messageSessionMapper;

    @Resource
    private MessageMapper messageMapper;
    @GetMapping("/sessionList")
    public Object getMessageSessionList(HttpServletRequest req){
        List<MessageSession> messageSessionList = new ArrayList<>();

        //1.获取到当前用户的UserId(spring 的 session 获取)
        HttpSession session = req.getSession(false);
        if(session == null){
            System.out.println("[getMessageSessionList] session == null");
            return messageSessionList;
        }
        User user = (User)  session.getAttribute("user");
        if(user == null){
            System.out.println("[getMessageSessionList] user == null");
            return messageSessionList;
        }

        //2.根据userId 查询数据库,查出来有些会话id
        List<Integer> sessionIdList = messageSessionMapper.getSessionByUserId(user.getUserId());
        System.out.println(sessionIdList.size());
        for (int sessionId : sessionIdList){
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);
            //3.遍历会话id,查现出每个会话里涉及到的好友有谁
            List<Friend> friends = messageSessionMapper.getFriendsBySessionId(sessionId, user.getUserId());
            messageSession.setFriends(friends);
            String lastMessage = messageMapper.getLasMessageBySessionId(sessionId);
            if(lastMessage == null){
                messageSession.setLastMessage("");
            }else {
                messageSession.setLastMessage(lastMessage);
            }


            messageSessionList.add(messageSession);
        }
        //最终目的就是构造出一个MessageSession对象数组
        return messageSessionList;
    }
    @PostMapping("/session")
    @Transactional
    public Object addMessageSession(int toUserId, @SessionAttribute("user") User user){
        HashMap<String,Integer> resp = new HashMap<>();
        //1.先给message_session表里插入记录.使用这个参数的目的 主要是为了获取到自增主键
        MessageSession messageSession = new MessageSession();
        messageSessionMapper.addMessageSession(messageSession);
        //2.给message_session_user 表插入记录
        MessageSessionUserItem item1 = new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setUserId(user.getUserId());
        messageSessionMapper.addMessageSessionUser(item1);
        //3
        MessageSessionUserItem item2 = new MessageSessionUserItem();
        item2.setSessionId(messageSession.getSessionId());
        item2.setUserId(toUserId);
        messageSessionMapper.addMessageSessionUser(item2);

        resp.put("sessionId", messageSession.getSessionId());
        return resp;
    }

}
